package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisMessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.Map;

/**
 * 快速登录管理
 */
@RestController
@RequestMapping("/login")
public class MemberController {

   @Autowired
   private JedisPool jedisPool;
    @Reference
   private MemberService memberService;

    @RequestMapping("/check")
    public Result login(HttpServletResponse response,@RequestBody Map map) {

        //获取前台输入的手机号与验证码
        String telephone = (String) map.get("telephone");
        String validateCode = (String) map.get("validateCode");
        //从redis中获取存入的验证码
        String validateRedisCode = jedisPool.getResource().get(telephone + RedisMessageConstant.SENDTYPE_LOGIN);
        //将短信发的验证码和前台验证码进行比对
        if (validateCode != null && validateRedisCode != null && validateCode.equals(validateRedisCode)) {
            //如果相同，调用业务功能进行录入登录信息
            //先根据手机判断是否已经有此会员
            Member member = memberService.findByTelephone(telephone);
            if (member == null) {
                //如果没有，则执行新增功能
                member = new Member();
                member.setPhoneNumber(telephone);
                member.setRegTime(new Date());
                memberService.add(member);
            }
            //将信息放到cookie中
            Cookie cookie = new Cookie("login_member_telephone", telephone);
            cookie.setPath("/");//应用服务器存放应用的文件夹的根目录
            cookie.setMaxAge(60*60*24*30);//设置存放时间
            response.addCookie(cookie);
            //将member信息存入redis
            String memberRedis = JSON.toJSONString(member);
            jedisPool.getResource().setex(telephone, 1800, memberRedis);
            //返回信息登陆成功
            return new Result(true, MessageConstant.LOGIN_SUCCESS);
        } else {
            //验证码比对有误
            return new Result(false, MessageConstant.VALIDATECODE_ERROR);
        }
    }
    @RequestMapping("/findMember")
    public Result findMember(HttpServletRequest request){
   //获得当前用户cookie
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            String name = cookie.getName();
            if (name.equals("login_member_telephone")){
                String memberRedis = jedisPool.getResource().get(cookie.getValue());
                if (memberRedis!=null){
                    Member member = JSON.parseObject(memberRedis, Member.class);
                    return new Result(true,"success",member);
                }

                Member byTelephone = memberService.findByTelephone(cookie.getValue());
                //将member信息存入redis
                String member = JSON.toJSONString(byTelephone);
                jedisPool.getResource().setex(cookie.getValue(), 1800, member);
                return new Result(true,"success",byTelephone);
            }


        }



        return new Result(false,"用户登录失效,请重新登录");
    }
    @RequestMapping("/editMember")
    public Result editMember(@RequestBody Member member){
        try {
            memberService.editMember(member);
            //更新Redis数据操作
            jedisPool.getResource().del(member.getPhoneNumber());
            return new Result(true,MessageConstant.EDIT_MEMBER_SUCCESS);
        }catch (Exception e){
            return new Result(false,MessageConstant.EDIT_MEMBER_FAIL);
        }
    }
}
